package MyStack;

import java.util.Arrays;
import java.util.Stack;

public class Mystack {

     public  int[]  elem;
     public int usedsize;

    public Mystack() {
        this.elem = new int[5] ;
    }
    public  void push (int val){
        if(isFull()){
            elem = Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedsize]=val;
        usedsize++;
    }
    public  boolean isFull(){
        if(usedsize==elem.length){
            return  true;
        }
        return false;
    }
    public  int pop(){
        if(isempty()){
            throw new RuntimeException("error");
        }
        int ret= elem[usedsize-1];
        usedsize--;
        return ret;
    }
    public int peek(){
        if(isempty()){
            throw  new RuntimeException("error");
        }
        return  elem[usedsize-1];
    }
    public  boolean isempty(){
        if(usedsize==0){
            return  true;
        }
        return false;
    }
    public int getUsedsize(){
        return  usedsize;
    }
    public  int houzhuipandaun(String[] arr){  //编写后缀程序
        Stack<Integer > stack =new Stack();
        for (int i=0;i<arr.length;i++){
            String x= arr[i];
            if(!houzhui(x)){
                stack.push(Integer.parseInt(x));
            }
            else {
                int num1 =stack.pop();
                int num2 =stack.pop();
                switch (x){
                    case "+":
                        stack.push(num1+num2);
                        break;
                    case "-":
                        stack.push(num1+num2);
                        break;
                    case "*":
                        stack.push(num1+num2);
                        break;
                    case "/":
                        stack.push(num1+num2);
                        break;
                }
            }
        }
        return stack.pop();
    }
    public boolean houzhui(String s){
        if(s.equals(("+"))||s.equals(("+"))||s.equals(("+"))||s.equals(("+"))){
            return true;
        }
        return false;
    }
    public  boolean isValid(String s){//判断括号对称
        Stack<Character> stack =new Stack<>();
        for (int i = 0; i <s.length() ; i++) {
            char ch =s.charAt(i);
            if(ch=='('||ch=='['||ch=='{'){
                stack.push(ch);
            }
            else {
                if (stack.empty()){
                    return  false;
                }
                char c =stack.peek();
                if(c=='('&&ch==')'||c=='['&&ch==']'||c=='{'&&ch=='}'){
                    stack.pop();
                }else {
                    return false;
                }
            }
        }
        if(stack.empty()){
            return true;
        }
        return false;
    }
    public  boolean isPopOrder(int[] pushA,int[] popB){  ///判断出栈是否属于入栈的顺序
        Stack<Integer> stack =new Stack<>();
        int j=0;

        if(pushA.length==0||popB.length==0){
            return false;
        }
        for (int i = 0; i < pushA.length; i++) {
            int val =pushA[i];
            stack.push(val);
            while (!stack.empty()&&j<popB.length&&stack.peek()==popB[j]){
                stack.pop();
                j++;
            }
        }
        if(stack.empty()){
            return true;
        }
        return false;
    }
    public boolean backspaceCompare(String s, String t) { //遇到指定符号，退格 栈解决
        //s = "ab#c", t = "ac#c"
        // 输出：true
         // 解释：s 和 t 都会变成 "ac"。
        Stack<Character> st1 = new Stack<>();
        Stack<Character> st2 = new Stack<>();
        for (Character c : s.toCharArray()) {
            if (c != '#') {
                st1.push(c);
            }
            else if (!st1.isEmpty()) {
                st1.pop();
            }
        }
        for (Character c : t.toCharArray()) {
            if (c != '#') {
                st2.push(c);
            }
            else if (!st2.isEmpty()) {
                st2.pop();
            }
        }
        while (!st1.isEmpty() && !st2.isEmpty()) {
            if (st1.pop() != st2.pop()) {
                return false;
            }
        }
        return st1.isEmpty() && st2.isEmpty();
    }
}
